#!/usr/bin/bash

######################################################################
# @description : OHOS 开发环境初始化脚本
######################################################################

set -ex

START_DIR=`realpath $PWD`
OHOS_BUILD_TOP=${START_DIR}/openharmony_workspace
LOCAL_BIN_DIR=$HOME/bin
export PATH=$LOCAL_BIN_DIR:$PATH

# 创建目录
if [ ! -d $LOCAL_BIN_DIR ]; then
    mkdir -p $LOCAL_BIN_DIR
fi
mkdir -p $OHOS_BUILD_TOP

# 设置Python 符号链接
if which python3 >/dev/null; then
    python_version_string=$(python3 --version | awk '{print $2}')
    python_cmd=`which python3`
elif which python >/dev/null; then
    python_version_string=$(python --version | awk '{print $2}')
    python_cmd=`which python`
fi
if [[ ! "$python_version_string" =~ ^3.* ]]; then
    echo "Please install Python-3.x"
    exit
else
    ln -sf `which ${python_cmd}` $LOCAL_BIN_DIR/python
fi

# 克隆 repo
pushd $OHOS_BUILD_TOP

if [ ! -f $LOCAL_BIN_DIR/repo ]; then
    curl https://gitee.com/oschina/repo/raw/main/repo -o $LOCAL_BIN_DIR/repo
    chmod +x $LOCAL_BIN_DIR/repo
fi

# 同步代码
if [ ! -f .repo/manifests/devboard_ls3a5000_v4.1.xml ]; then
    repo init -u git@gitee.com:ohos4la-l2/manifest.git -b ohos-4.1 -m devboard_ls3a5000_v4.1.xml --no-repo-verify -v
fi

repo sync -c
repo forall -c 'git lfs pull'
./build/prebuilts_download.sh

# 编译项目
bash -x ./build.sh --product-name ls3a5000 --ccache --no-prebuilt-sdk 2>&1 | tee build.log

# 单独编译项目组件 musl
# bash -x ./build.sh --product-name ls3a5000 --build-target musl --ccache --no-prebuilt-sdk 2>&1 | tee build.one.log
